#include "m_studentmanage.h"
#include "ui_m_studentmanage.h"
#include "m_studentinsert.h"
#include "m_studentupdate.h"

QString global_id;

M_studentManage::M_studentManage(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::M_studentManage)
{
    ui->setupUi(this);
    querytable();
   // connect(this,SIGNAL(sendMessage()),_interface,SLOT(getMessage()));
}

M_studentManage::~M_studentManage()
{
    delete ui;
}

//表查询
void  M_studentManage::querytable()
{
    QString str=QString("select * from student");
    model.setQuery(str);
    ui->tableView->setModel(&model);
    model.setHeaderData(0,Qt::Horizontal,"学号");
    model.setHeaderData(1,Qt::Horizontal,"姓名");
    model.setHeaderData(2,Qt::Horizontal,"性别");
    model.setHeaderData(3,Qt::Horizontal,"电话");
    model.setHeaderData(4,Qt::Horizontal,"院系");
    model.setHeaderData(5,Qt::Horizontal,"专业");
    model.setHeaderData(6,Qt::Horizontal,"房间号");
    model.setHeaderData(7,Qt::Horizontal,"楼栋号");
    model.setHeaderData(8,Qt::Horizontal,"密码");
}
//插入
void M_studentManage::on_insertBtn_clicked()
{
    M_studentInsert window;
    window.exec();
    querytable();

}

//更新
void M_studentManage::on_updateBtn_clicked()
{
    int row = ui-> tableView ->currentIndex().row();
    QString id=model.data(model.index(row,0)).toString();

    global_id=id;
    m_studentUpdate window;
    window.exec();
    querytable();
}

//删除
void M_studentManage::on_deleteBtn_clicked()
{
    int row = ui-> tableView ->currentIndex().row();
    QString id=model.data(model.index(row,0)).toString();

    QSqlQuery query;
    QString str=QString("delete from student where sid='%1'").arg(id);
    if(query.exec(str)){
        QMessageBox::information(NULL,"tips","删除成功");
        querytable();
    }
    else {
        QMessageBox::warning(NULL,"Error","删除失败");
    }
}

